import {toast} from "../../utils/tool";
import {BASE_URL} from '../../constant/index'
const app = getApp();
Page({
  data: {
    jiaocheng: '',
    content: '',
    title: '',
  },
  generateRichText(content) {
    let imgReg = RegExp(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi);//定义正则，筛选出img元素
    let matchRes = content.match(imgReg);
    if (matchRes) {
      matchRes.forEach((item, index) => {
        let _tempStr = item.slice(0, item.length - 2);
        let _index = _tempStr.indexOf('/');
        let _str = _tempStr.substring(0, _index) + BASE_URL + _tempStr.substring(_index, _tempStr.length) + '"/>';
        content = content.replace(item, _str)
      })
    }
    return content
  },
  onLoad(options) {
    app.ajax({
      url: '/api/content/instruction',
      data: {
        type: 1
      },
      success: res => {
        const {code, data, msg} = res
        if (code !== 1) return toast({title: msg})
        const [targetData] = data
        const content = this.generateRichText(targetData.content)
        this.setData({
          title: targetData.title,
          content,
        });
      }
    })
  },
})
